package com.edu.leetcode.dataStruct;


import java.util.*;

/**
 * @作者 five-five
 * @创建时间 2021/6/8
 */
public class CodePointConversion3 {
    private String regix = "^([-+]?(([0-9]+)([.]([0-9]+))?|([.]([0-9]+))?))$";

    public static void main(String[] args) {
        String str1 = "sssss";
        String str2 = "good";
        String encode = "GB2312";
        String s1 = "2502 A 2502 B";
        String ss1 = "2502 A 2502 2494 B";
        String sss1 = "2502 A 2502 2494 2502";
        String ssss1 = "2502 (2502 2502";
        String sssss1 = "2502 2502)";
        String ssssss1 = "2502 (2502 2502) 2502";
        String sssssss1 = "2502) 2502";
        String sssssss2 = "           ";
        CodePointConversion3 codePointConversion = new CodePointConversion3();
//        System.out.println(codePointConversion.convertText(str1));
//        System.out.println(codePointConversion.convertText(str2));
//        System.out.println(codePointConversion.convertText(encode));
//        System.out.println(codePointConversion.convertText(s1));
//        System.out.println(codePointConversion.convertText(ss1));
//        System.out.println(codePointConversion.convertText(sss1));
//        System.out.println(codePointConversion.convertText(ssss1));
//        System.out.println(codePointConversion.convertText(sssss1));
//        System.out.println(codePointConversion.convertText(ssssss1));
//        System.out.println(codePointConversion.convertText(sssssss1));
        System.out.println(codePointConversion.convertText(sssssss2));
    }

    /**
     * @param str e.g. (2502) A 2502 A 2502
     * @return 转换后的简体
     */
    public String convertText(String str) {
        if (str.substring(0, 1).equals(" ")) {
            str = str.substring(1);
        }
        String[] s = str.split(" ");
        int length = s.length;
        StringBuilder stringBuilder = new StringBuilder(length);
        LinkedHashMap<Integer, Boolean> map = new LinkedHashMap<>();
        for (int i = 0; i < length; i++) {
            map.put(i, isOurWant(s[i]));
        }
        Set<Map.Entry<Integer, Boolean>> entries = map.entrySet();
        for (Map.Entry<Integer, Boolean> entry : entries) {
            if (!entry.getValue()) {
                if (entry.getKey() - 1 >= 0) {
                    stringBuilder.append(s[entry.getKey() - 1]);
                    s[entry.getKey() - 1] = "";
                }
                stringBuilder.append(s[entry.getKey()]);
                s[entry.getKey()] = "";
            }
        }
        for (String s1 : s) {
            if (s1 == null || s1.isEmpty()) {
                continue;
            }
            char first = s1.charAt(0);
            char last = s1.charAt(s1.length() - 1);
            boolean flag = false;
            if (first == '(') {
                s1 = s1.substring(1);
                stringBuilder.append("(");
            }
            if (last == ')') {
                s1 = s1.substring(0, s1.length() - 1);
                flag = true;
                stringBuilder.append(")");
            }
            if (flag) {
//                stringBuilder.insert(stringBuilder.length() - 1, Character.toChars(Integer.parseInt(s1)));
                stringBuilder.insert(stringBuilder.length() - 1, selectStptswfBySwfTele(s1,true));
            } else {
//                stringBuilder.append(Character.toChars(Integer.parseInt(s1)));
                stringBuilder.append(selectStptswfBySwfTele(s1,true));
            }
            flag = false;
        }
        return stringBuilder.toString();
    }

    /**
     * 暂时性的转换方法
     *
     * @param elecCode 码，就String
     * @param isSim    是否是简体，默认为true
     * @return
     */
    public String selectStptswfBySwfTele(String elecCode, boolean isSim) {
        if (isSim == true) {
            String sim = "周";
            if (elecCode.equals("2502") || elecCode.equals("2494")) {
                return sim;
            } else {
                return elecCode;
            }
        } else {
            String tra = "陽";
            if (elecCode.equals("2502") || elecCode.equals("2494")) {
                return tra;
            } else {
                return elecCode;
            }
        }

    }


    /**
     * 判断该字符是否是想要的
     *
     * @param str
     * @return
     */
    private boolean isOurWant(String str) {
        if (str.length() >= 4) {
            if (str.matches(regix)) {
                return true;
            }
            char c = str.charAt(0);
            char c1 = str.charAt(str.length() - 1);
            if (c == '(') {
                str = str.substring(1);
            }
            if (c1 == ')') {
                str = str.substring(0, str.length() - 1);
            }
            boolean matches = str.matches(regix);
            return matches;
        }
        return false;
    }

}
